<template>
    <el-card>
        <el-form inline>
            <el-form-item label="一级分类">
                <el-select clearable v-model="IDc1" :disabled="switchform">
                    <el-option v-for="c1 in c1Arr" :key="c1.id" :label="c1.name" :value="c1.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="二级分类">
                <el-select clearable v-model="IDc2" :disabled="switchform">
                    <el-option v-for="c2 in c2Arr" :key="c2.id" :label="c2.name" :value="c2.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="三级分类">
                <el-select clearable v-model="IDc3" :disabled="switchform">
                    <el-option v-for="c3 in c3Arr" :key="c3.id" :label="c3.name" :value="c3.id" />
                </el-select>
            </el-form-item>
        </el-form>
        <!-- {{ switchform }} -->
    </el-card>
</template>

<script setup lang="ts">
import { reqC1, reqC2, reqC3 } from '@/apis/product/attr'
import { onMounted, ref, watch, watchEffect } from 'vue'

// import useCategoryStore from '@/store/modules/category';
// let categoryStore = useCategoryStore()
const c1Arr = ref<any>([])
const c2Arr = ref<any>([])
const c3Arr = ref<any>([])
const IDc1 = ref<number | string>('')
const IDc2 = ref<number | string>('')
const IDc3 = ref<number | string>('')

// defineExpose([IDc1, IDc2, IDc3])
defineExpose({ IDc1, IDc2, IDc3 })
defineProps(['switchform'])
//分类全局组件挂载完毕,通知仓库发请求获取一级分类的数据

//通知仓库获取一级分类的方法
const getC1 = async () => {
    let res = await reqC1()
    if (res.code === 200) { c1Arr.value = res.data }
}
onMounted(() => {
    getC1()
})
watch([IDc1, IDc2], async (newValue, oldValue) => {
    // console.log(newValue, oldValue)
    if (IDc1.value !== oldValue[0]) {
        let res = await reqC2(IDc1.value)
        if (res.code === 200) { c2Arr.value = res.data }
        IDc2.value = ''
        IDc3.value = ''
        // console.log('c1', res);
    } else {
        if (IDc2.value === '') return
        let res = await reqC3(IDc2.value)
        if (res.code === 200) { c3Arr.value = res.data }
        IDc3.value = ''
        // console.log('c2', res);
    }
})

</script>

<style lang="scss">
.el-select {
    width: 15vw;
}
</style>